{include file='admin/header.html'}

<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">{$tran.role_name}</label>
        <div class="layui-input-inline">
            <input type="text" name="name" value="{$role.name}" required lay-verify="required" placeholder="{$tran.input_role_name}"
                autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">{$tran.auth_assign}</label>
        <div class="layui-input-block">
            <div id="dataTree"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">{$tran.sort}</label>
        <div class="layui-input-inline">
            <input type="number" name="sort" value="{$role.sort}" required lay-verify="required|number"
                placeholder="{$tran.input_sort}" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">{$tran.status}</label>
        <div class="layui-input-block">
            <input type="hidden" name="status" value="1">
            <input type="checkbox" name="status" value="0" lay-skin="switch" {if $role.status==0} checked{/if}{if $role.id==1} disabled{/if}
                lay-text="ON|OFF">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <input type="hidden" name="id" value="{$role.id}">
            <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">{$tran.save}</button>
            <button type="reset" class="layui-btn layui-btn-primary">{$tran.reset}</button>
        </div>
    </div>
</form>

<script>
    layui.use(['tree'], function (args) {
        var tree = layui.tree,
            $ = layui.jquery;

        var data = {$auth_menu},
            values = {$role.auth_ids};

        // 树形组件
        tree.render({
            elem: '#dataTree',
            checkChirld: false,
            data: data,
            showCheckbox: true,
        });
        $('#dataTree').find('input[type="checkbox"]').each(function () {
            var value = $(this).attr('value');
            // if (values.find(function (item) {
            //     return item == value;
            // })) {
            //     $(this).attr('checked', true);
            //     $(this).parent().find('.layui-form-checkbox').addClass('layui-form-checked');
            // }
            // IE10兼容
            for (var i in values) {
                if (values[i] == value) {
                    $(this).attr('checked', true);
                    $(this).parent().find('.layui-form-checkbox').addClass('layui-form-checked');
                }
            }
            $(this).attr('name', 'auth_ids[]');
        });
    });
</script>

{include file='admin/inc/form.html'}

{include file='admin/footer.html'}